package com.gmss.crawler.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gmss.crawler.constants.Constants;
import com.gmss.crawler.enums.AcademicianEnum;
import com.gmss.crawler.module.paper.domain.Paper;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.module.resume.domain.Resume;
import com.gmss.crawler.module.social.domain.Social;
import com.gmss.crawler.utils.CommonUtils;
import com.gmss.crawler.utils.CrawlerCommonUtils;
import com.gmss.crawler.utils.OkHttp3Util;
import com.gmss.crawler.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * 页面URL：https://gct.aminer.cn/
 * aminer-全球华人专家库列表
 */
@Slf4j
@Service
public class AminerCrawlerService {

    @Value("${outstand_youth_path}")
    private String outstandYouthPath;

    /**
     * 全球华人专家库-杰青年度列表
     *
     * @return
     */
    public static List<Map<String, Object>> aminerList() {
        List<Map<String, Object>> list = new ArrayList<>();
        String requestStr = null;
        requestStr = "[{\"action\":\"expertbase.search.Global\",\"parameters\":{\"filters\":{\"nested\":{\"path\":\"labels\"," +
                "\"query\":{\"match_phrase\":{\"labels.value\":\"杰青系列\"}}}," +
                "\"must_not\":{\"is_public\":false}}," +
                "\"offset\":0,\"size\":1000}," +
                "\"schema\":{\"expertbase\":[\"name\",\"name_zh\",\"logo\",\"type\",\"stats\",\"is_deleted\",\"parents\",\"is_public\",\"price\",\"report_link\"" +
                ",\"order\",\"desc\",\"desc_zh\",\"created_time\",\"updated_time\",\"creator\",\"system\",\"related_venues\",\"labels\",\"all_stats\"]}}]";


        try {
            Thread.sleep(CommonUtils.randomRange(100, 500));
            okhttp3.Response response = OkHttp3Util.doPostJSON(Constants.AMINER_JQ_LIST_PATH, requestStr, 5000, 3, false);
            String result = response.body().string();
            if (StringUtils.isNotBlank(result)) {
                JSONObject resultJson = JSON.parseObject(result);
                if (resultJson.containsKey("data") && resultJson.getJSONArray("data") != null && resultJson.getJSONArray("data").size() > 0) {
                    JSONArray dataArray = resultJson.getJSONArray("data");
                    JSONObject jsonObject = dataArray.getJSONObject(0);
                    if (null != jsonObject && jsonObject.getBoolean("succeed") && jsonObject.getInteger("total") > 0) {
                        // 保存全球华人专家库-杰青年度列表信息
                        list = parseAminerListItems(jsonObject);
                    }
                }
            }
        } catch (IOException e) {
            log.error("全球华人专家库-杰青年度列表-aminerList, Exception：{}", e);
        } catch (InterruptedException e) {
            log.error("全球华人专家库-杰青年度列表-aminerList, Exception：{}", e);
        }
        return list;
    }


    /**
     * 全球华人专家库-杰青年度列表信息
     *
     * @param jsonObject
     * @return
     */
    private static List<Map<String, Object>> parseAminerListItems(JSONObject jsonObject) {
        List<Map<String, Object>> list = new ArrayList<>();
        JSONArray itemArrays = jsonObject.getJSONArray("items");
        for (int i = 0; i < itemArrays.size(); i++) {
            Map<String, Object> resultMap = new LinkedHashMap<>();
            JSONObject itemJsonObject = itemArrays.getJSONObject(i);
            String nameZh = itemJsonObject.containsKey("name_zh") ? itemJsonObject.getString("name_zh") : "";
            String id = itemJsonObject.containsKey("id") ? itemJsonObject.getString("id") : "";
            if (StringUtils.isBlank(nameZh)) {
                nameZh = itemJsonObject.containsKey("name") ? itemJsonObject.getString("name") : "";
            }
            if (!StringUtils.isAllBlank(nameZh, id)) {
                nameZh = RegexUtils.getTextContent(nameZh, RegexUtils.ALL_ZHONG_REGEX_TWO);
                if (nameZh.contains("年") && nameZh.indexOf("年") > 0) {
                    nameZh = nameZh.replaceAll("年", "");
                }
                // 年
                resultMap.put("year", nameZh);
                // 列表id
                resultMap.put("listId", id);
                list.add(resultMap);
            }
        }
        return list;
    }


    /**
     * 杰青-1994 至 2020列表数据
     *
     * @param result
     * @param nameMap
     * @param year
     */
    public static void parseAminerTxt(String result, Map<String, PersonInfo> nameMap, String year) {
        if (StringUtils.isNotBlank(result)) {
            JSONObject resultJson = JSON.parseObject(result);
            if (resultJson.containsKey("data") && resultJson.getJSONArray("data") != null && resultJson.getJSONArray("data").size() > 0) {
                JSONArray dataArray = resultJson.getJSONArray("data");
                JSONObject jsonObject = dataArray.getJSONObject(0);
                if (null != jsonObject && jsonObject.getBoolean("succeed") && jsonObject.getInteger("total") > 0) {
                    // 全球华人专家库-杰青年度人员信息列表-解析
                    parseItems(nameMap, jsonObject, year);
                }
            }
        }
    }

    /**
     * 全球华人专家库-杰青年度人员信息列表-解析
     *
     * @param nameMap
     * @param jsonObject
     * @param year
     */
    private static void parseItems(Map<String, PersonInfo> nameMap, JSONObject jsonObject, String year) throws NullPointerException {
        JSONArray itemArrays = jsonObject.getJSONArray("items");
//        PersonInfo personInfo = new PersonInfo();
        for (int i = 0; i < itemArrays.size(); i++) {
            JSONObject itemJsonObject = itemArrays.getJSONObject(i);
            parseAminerPersonInfo(nameMap, year, itemJsonObject);
        }
    }

    private static PersonInfo parseAminerPersonInfo(JSONObject itemJsonObject, PersonInfo personInfo) {
        // 中文名
        String nameZh = itemJsonObject.containsKey("name_zh") ? itemJsonObject.getString("name_zh") : null;
        // 外文名
        String foreignName = itemJsonObject.containsKey("name") ? itemJsonObject.getString("name") : null;
        // adminer个人详情页用到id
        String aminerId = itemJsonObject.containsKey("id") ? itemJsonObject.getString("id") : null;
        // {"relatedLinksName":"AMiner","relatedLinksUrl":"https://www.aminer.cn/profile/-/542dfe4cdabfaed722682540"}

        // 个人图片地址
        String imgPath = itemJsonObject.containsKey("avatar") ? itemJsonObject.getString("avatar") : null;
//        String homepage = itemJsonObject.containsKey("homepage") ? itemJsonObject.getString("homepage") : null;
        // 相关链接
        JSONArray jsonArray = new JSONArray();
        JSONObject linksJsonObject = itemJsonObject.containsKey("links") ? itemJsonObject.getJSONObject("links") : null;
        if (null != linksJsonObject && linksJsonObject.size() > 0) {
            JSONObject gsJson = linksJsonObject.containsKey("gs") ? linksJsonObject.getJSONObject("gs") : null;
            if (null != gsJson && gsJson.size() > 0) {
                String url = gsJson.containsKey("url") ? gsJson.getString("url") : null;
                if (StringUtils.isNotBlank(url)) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("relatedLinksName", "gs");
                    jsonObject.put("relatedLinksUrl", url);
                    jsonArray.add(jsonObject);
                }
            }
            JSONObject resourceJson = linksJsonObject.containsKey("resource") ? linksJsonObject.getJSONObject("resource") : null;
            if (null != resourceJson && resourceJson.size() > 0) {
                JSONArray resourceLinkArray = resourceJson.containsKey("resource_link") ? resourceJson.getJSONArray("resource_link") : null;
                if (null != resourceLinkArray && resourceLinkArray.size() > 0) {
                    for (Object object : resourceLinkArray) {
                        JSONObject resourceLinkJsonObject = (JSONObject) object;
                        String resourceLinkUrl = resourceLinkJsonObject.containsKey("url") ? resourceLinkJsonObject.getString("url") : null;
                        if (StringUtils.isNotBlank(resourceLinkUrl)) {
                            String resourceLinkId = resourceLinkJsonObject.containsKey("id") ? resourceLinkJsonObject.getString("id") : null;
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("relatedLinksName", resourceLinkId);
                            jsonObject.put("relatedLinksUrl", resourceLinkUrl);
                            jsonArray.add(jsonObject);
                        }
                    }
                }
            }
        }

        List tagsTranslatedZh = itemJsonObject.containsKey("tags_translated_zh") ? itemJsonObject.getJSONArray("tags_translated_zh") : null;
        if (CollectionUtils.isEmpty(tagsTranslatedZh))
            tagsTranslatedZh = itemJsonObject.containsKey("tags_translated") ? itemJsonObject.getJSONArray("tags_translated") : null;
        String workCompany = null;
        String position = null;
        String researchContents = null;
        String sex = null;
        String homepage = null;
        String bio = null;
        String edu = null;
        List<Social> socialList = new ArrayList<>();
        List<Resume> resumeList = new ArrayList<>();
        JSONObject profileJSONObject = itemJsonObject.containsKey("profile") ? itemJsonObject.getJSONObject("profile") : null;
        if (null != profileJSONObject) {

            bio = profileJSONObject.containsKey("bio_zh") ? profileJSONObject.getString("bio_zh") : null;
            if (StringUtils.isBlank(bio)) {
                bio = profileJSONObject.containsKey("bio") ? profileJSONObject.getString("bio") : null;
            }
            homepage = profileJSONObject.containsKey("homepage") ? profileJSONObject.getString("homepage") : null;
            // 工作单位
            workCompany = profileJSONObject.containsKey("affiliation_zh") ? profileJSONObject.getString("affiliation_zh") : null;
            if (StringUtils.isBlank(workCompany)) {
                workCompany = profileJSONObject.containsKey("affiliation") ? profileJSONObject.getString("affiliation") : null;
            }
            // 职称
            position = profileJSONObject.containsKey("position_zh") ? profileJSONObject.getString("position_zh") : null;
            if (StringUtils.isBlank(position))
                position = profileJSONObject.containsKey("position") ? profileJSONObject.getString("position") : null;
            // 性别，male：男，female：女
            String gender = profileJSONObject.containsKey("gender") ? profileJSONObject.getString("gender") : null;
            if (StringUtils.equals(gender, "male")) {
                sex = "男";
            } else if (StringUtils.equals(gender, "female")) {
                sex = "女";
            }
            // 社会任职
            String social = profileJSONObject.containsKey("work_zh") ? profileJSONObject.getString("work_zh") : null;
            if (StringUtils.isBlank(social))
                social = profileJSONObject.containsKey("work") ? profileJSONObject.getString("work") : null;
            social = RegexUtils.replaceData(social, "\\n", "<br>");
            handlerSocial(social, socialList);

            edu = profileJSONObject.containsKey("edu") ? profileJSONObject.getString("edu") : null;
            if (StringUtils.isBlank(edu))
                edu = profileJSONObject.containsKey("edu_zh") ? profileJSONObject.getString("edu_zh") : null;
            edu = RegexUtils.replaceData(edu, "\\n", "<br>");
            handlerResume(edu, resumeList);

        }
        if (CollectionUtils.isNotEmpty(tagsTranslatedZh)) {
            for (int i = tagsTranslatedZh.size() - 1; i >= 0; i--) {
                if (ObjectUtils.isEmpty(tagsTranslatedZh.get(i))) {
                    tagsTranslatedZh.remove(i);
                }
            }
            researchContents = StringUtils.join(tagsTranslatedZh, ",");
        }
        String companyType = CasCrawlerService.getCompanyType(workCompany);

        if (StringUtils.isBlank(personInfo.getDescribeData()) && StringUtils.isNotBlank(bio)) {
            personInfo.setDescribeData(bio);
            Map<String, String> map = new HashMap<>();
            new BaiduBaiKeCrawlerService().setProperty(map, bio, "学位");
            if (MapUtils.isNotEmpty(map) && map.containsKey("学位"))
                personInfo.setAcademicDegree(map.get("学位"));

            // 出生日期
            new BaiduBaiKeCrawlerService().getBirthdayByResume(personInfo, bio);

            // 出生地
            new BaiduBaiKeCrawlerService().getBirthplaceByResume(personInfo, bio);


            if (StringUtils.containsAny(bio, "加入中国共产党", "中国共产党党员", "中共党员")) {
                personInfo.setPoliticalParty("中国共产党");
            }
        }
        if (StringUtils.isBlank(personInfo.getDescribeData()) && StringUtils.isNotBlank(edu)) {
            Map<String, String> map = new HashMap<>();
            if (StringUtils.isBlank(personInfo.getAcademicDegree()) && StringUtils.isNotBlank(edu)) {
                new BaiduBaiKeCrawlerService().setProperty(map, edu, "学位");
                if (MapUtils.isNotEmpty(map) && map.containsKey("学位"))
                    personInfo.setAcademicDegree(map.get("学位"));
            }
            new BaiduBaiKeCrawlerService().getBirthdayByResume(personInfo, edu);
            new BaiduBaiKeCrawlerService().getBirthplaceByResume(personInfo, edu);
            if (StringUtils.containsAny(edu, "加入中国共产党", "中国共产党党员", "中共党员")) {
                personInfo.setPoliticalParty("中国共产党");
            }
        }



        if (StringUtils.isBlank(personInfo.getName()) && StringUtils.isNotBlank(nameZh))
            personInfo.setName(nameZh);
        if (StringUtils.isBlank(personInfo.getForeignName()) && StringUtils.isNotBlank(foreignName))
            personInfo.setForeignName(foreignName);
        if (StringUtils.isBlank(personInfo.getImgName()) && StringUtils.isNotBlank(imgPath))
            personInfo.setImgName(imgPath);
        if (StringUtils.isBlank(personInfo.getPersonTitle()) && StringUtils.isNotBlank(position))
            personInfo.setPersonTitle(position);
        if (StringUtils.isBlank(personInfo.getResearchContents()) && StringUtils.isNotBlank(researchContents))
            personInfo.setResearchContents(researchContents);
        if (StringUtils.isBlank(personInfo.getWorkCompany()) && StringUtils.isNotBlank(workCompany))
            personInfo.setWorkCompany(workCompany);
        if (StringUtils.isBlank(personInfo.getCompanyType()) && StringUtils.isNotBlank(companyType))
            personInfo.setCompanyType(companyType);
        if (StringUtils.isBlank(personInfo.getSex()) && StringUtils.isNotBlank(sex))
            personInfo.setSex(sex);
        if (CollectionUtils.isEmpty(personInfo.getSocialList()) && CollectionUtils.isNotEmpty(socialList))
            personInfo.setSocialList(socialList);
        if (CollectionUtils.isEmpty(personInfo.getResumeList()) && CollectionUtils.isNotEmpty(resumeList))
            personInfo.setResumeList(resumeList);
        if (StringUtils.isNotBlank(aminerId)) {
            JSONObject relatedLinksJson = new JSONObject();
            relatedLinksJson.put("relatedLinksName", "AMiner");
            relatedLinksJson.put("relatedLinksUrl", "https://www.aminer.cn/profile/" + aminerId);
            jsonArray.add(relatedLinksJson);
        }
        if (StringUtils.isNotBlank(homepage)) {
            JSONObject relatedLinksJson = new JSONObject();
            relatedLinksJson.put("relatedLinksName", "homepage");
            relatedLinksJson.put("relatedLinksUrl", homepage);
            jsonArray.add(relatedLinksJson);
        }

        if (null != jsonArray && jsonArray.size() > 0 && StringUtils.isBlank(personInfo.getRelatedLinks())) {
            personInfo.setRelatedLinks(JSON.toJSONString(jsonArray));
        } else if (null != jsonArray && jsonArray.size() > 0) {
            String relatedLinks = personInfo.getRelatedLinks();
            if (StringUtils.isNotBlank(relatedLinks)) {
                JSONArray sourceArray = JSON.parseArray(relatedLinks);
                for (int i = 0; i < sourceArray.size(); i++) {
                    JSONObject linkJson = sourceArray.getJSONObject(i);
                    String relatedLinksName = linkJson.getString("relatedLinksName");
                    for (int j = 0; j < jsonArray.size(); j++) {
                        JSONObject pageLinkJson = jsonArray.getJSONObject(i);
                        String pageRelatedLinksName = pageLinkJson.getString("relatedLinksName");
                        if (StringUtils.equalsIgnoreCase(relatedLinksName, pageRelatedLinksName)) {
                            sourceArray.add(pageLinkJson);
                        }
                    }
                }
                personInfo.setRelatedLinks(JSON.toJSONString(sourceArray));
            }
        }

        if (StringUtils.isNotBlank(aminerId))
            personInfo.setAminerId(aminerId);

        return personInfo;
    }

    private static void parseAminerPersonInfo(Map<String, PersonInfo> nameMap, String year, JSONObject itemJsonObject) {
        PersonInfo personInfo = new PersonInfo();
        parseAminerPersonInfo(itemJsonObject, personInfo);
        if (StringUtils.isBlank(personInfo.getElectionYear()) && StringUtils.isNotBlank(year))
            personInfo.setElectionYear(year);
        if (null == personInfo.getAcademicianTitle())
            personInfo.setAcademicianTitle(AcademicianEnum.OUTSTANDING_YOUTH.getCode());
        if (StringUtils.isBlank(personInfo.getAcademicianTitleValue()))
            personInfo.setAcademicianTitleValue(AcademicianEnum.OUTSTANDING_YOUTH.getValue());
        if (null == personInfo.getTitleYears())
            personInfo.setTitleYears(AcademicianEnum.OUTSTANDING_YOUTH.getTitleYears());

        if (StringUtils.isNotBlank(personInfo.getName()) && nameMap.containsKey(personInfo.getName())) {
            nameMap.put(personInfo.getName(), personInfo);
        }
    }

    public static void getAminerPaperAndPatent() {
//        Document adminerDoc = CrawlerCommonUtils.getDocumentByFile("C:\\Users\\user\\Desktop\\53f7926bdabfae938c6c1610.html");
        Document adminerDoc = CrawlerCommonUtils.getDocumentByFile("C:\\Users\\user\\Desktop\\561a886045cedb3397e83e7d.html");
//        Document adminerDoc = CrawlerCommonUtils.getDocument("https://www.aminer.cn/profile/53f7926bdabfae938c6c1610");
        getAminerHtmlScript(adminerDoc);

    }

    private static String getAminerHtmlScript(Document adminerDoc) {
        PersonInfo personInfo = new PersonInfo();
        String researchContents = "";
        Elements allScript = adminerDoc.getElementsByTag("script");
        for (int i = 0; i < allScript.size(); i++) {
            Element elementScript = allScript.get(i);
            if (StringUtils.contains(elementScript.toString(), "window.g_initialProps")) {
                String loadingImgContainer = elementScript.toString();
                String[] initialPropsScript = loadingImgContainer.split("\\r\\n");
                JSONObject resultJson = null;
                for (int j = 0; j < initialPropsScript.length; j++) {
                    if (StringUtils.contains(initialPropsScript[j], "window.g_initialProps")) {
                        String scriptJson = initialPropsScript[j];
                        String resultStr = scriptJson.substring(scriptJson.indexOf("{"), scriptJson.lastIndexOf("}") + 1);
                        resultStr = CommonUtils.jsonUnicodeToUtf8(resultStr);
                        System.out.println(resultStr);
                        resultJson = JSONObject.parseObject(resultStr);
                    }
                }
                if (resultJson != null && resultJson.size() > 0) {
                    JSONObject personJson = resultJson.getJSONObject("profile").getJSONObject("profile");
                    parseAminerPersonInfo(personJson, personInfo);

                    JSONArray papersJsonArray = resultJson.getJSONObject("profile").getJSONArray("profilePubs");
                    parseAminerPaperItems(papersJsonArray, personInfo);

                    JSONArray patentsJsonArray = resultJson.getJSONObject("profile").getJSONArray("profilePatents");
                    for (int j = 0; j < patentsJsonArray.size(); j++) {
                        JSONObject patentJson = patentsJsonArray.getJSONObject(i);
                        String appDate = patentJson.containsKey("app_date") ? patentJson.getString("app_date") : null;
                        String appNumber = patentJson.containsKey("app_number") ? patentJson.getString("app_number") : null;
                        String patentTitle = patentJson.containsKey("title_zh") ? patentJson.getString("title_zh") : null;
                        List applicantsList = patentJson.containsKey("applicants") ? patentJson.getJSONArray("applicants") : null;
                        List<String> authorList = new ArrayList<>();
                        if (CollectionUtils.isNotEmpty(applicantsList)) {

                        }
//                        patentJson.containsKey("") ? patentJson.getString("") : null;
                    }
                    System.out.println(resultJson);
                }
//                scriptStr = loadingImgContainer.substring(loadingImgContainer.indexOf("["), loadingImgContainer.indexOf("]") + 1);
            }
        }

//        if (StringUtils.isNotBlank(scriptStr)) {
//            JSONArray jsonArray = JSON.parseArray(scriptStr);
//            List<String> list = new ArrayList<>();
//            for (Object object : jsonArray) {
//                JSONObject jsonObject = JSONObject.parseObject(object.toString());
//                list.add(jsonObject.getString("tagName"));
//            }
//            researchContents = StringUtils.join(list, ",");
//        }
        return researchContents;
    }


    /**
     * 解析全球华人库-论文列表
     *
     * @param paperItems
     */
    public static void parseAminerPaperItems(JSONArray paperItems, PersonInfo personInfo) {
        List<Paper> paperList = new ArrayList<>();
        for (int i = 0; i < paperItems.size(); i++) {
            JSONObject jsonObject = paperItems.getJSONObject(i);
            Paper paper = parsePaperData(jsonObject);
            if (null != paper && StringUtils.isNotBlank(paper.getTitle()) && StringUtils.isNotBlank(paper.getPublicationDate()))
                paperList.add(paper);
        }

        if (CollectionUtils.isNotEmpty(paperList)) {
            personInfo.setPaperList(paperList);
        }
    }

    /**
     * 解析论文
     *
     * @param jsonObject
     * @return
     */
    public static Paper parsePaperData(JSONObject jsonObject) {
        Paper paper = new Paper();
        // 语言，en：外文，zh：中文
        String lang = jsonObject.containsKey("lang") ? jsonObject.getString("lang") : null;
        if (StringUtils.isNotBlank(lang) && StringUtils.equalsIgnoreCase(lang, "en")) {
            // 外文
            paper.setLanguageStandard(1);
            parsePaperDataByEn(jsonObject, paper);
        } else if (StringUtils.isNotBlank(lang) && StringUtils.equalsIgnoreCase(lang, "zh")) {
            // 中文
            paper.setLanguageStandard(0);
            parsePaperDataByZh(jsonObject, paper);
        } else {
            parsePaperDataByZh(jsonObject, paper);
            parsePaperDataByEn(jsonObject, paper);
        }

        return paper;
    }

    /**
     * 中文
     *
     * @param jsonObject
     * @param paper
     */
    private static void parsePaperDataByZh(JSONObject jsonObject, Paper paper) {

        List urlList = jsonObject.containsKey("urls") ? jsonObject.getJSONArray("urls") : null;
        if (CollectionUtils.isNotEmpty(urlList) && StringUtils.isBlank(paper.getResourceLink()))
            paper.setResourceLink(StringUtils.join(urlList, ","));

        String doi = jsonObject.containsKey("doi") ? jsonObject.getString("doi") : null;
        // 发表日期
        String publicationDate = jsonObject.containsKey("year") ? jsonObject.getString("year") : null;
        // 摘要
        String abstractData = jsonObject.containsKey("abstract_zh") ? jsonObject.getString("abstract_zh") : null;
        // 标题
        String title = jsonObject.containsKey("title_zh") ? jsonObject.getString("title_zh") : null;

        JSONObject venueJSONObject = jsonObject.containsKey("venue") ? jsonObject.getJSONObject("venue") : null;
        if (null != venueJSONObject && venueJSONObject.size() > 0) {
            JSONObject venueInfoJSONObject = venueJSONObject.containsKey("info") ? venueJSONObject.getJSONObject("info") : null;
            if (null != venueInfoJSONObject && venueInfoJSONObject.size() > 0) {
                // 发表期刊
                String periodicalName = venueInfoJSONObject.containsKey("name_zh") ? venueInfoJSONObject.getString("name_zh") : null;
                // 出版人(或机构)
                String publisher = venueInfoJSONObject.containsKey("publisher") ? venueInfoJSONObject.getString("publisher") : null;
                if (StringUtils.isNotBlank(periodicalName))
                    paper.setPeriodical(periodicalName);
                if (StringUtils.isNotBlank(publisher))
                    paper.setPublisher(publisher);
            }
        }

        // 作者
        JSONArray authorsJSONArray = jsonObject.containsKey("authors") ? jsonObject.getJSONArray("authors") : null;
        if (null != authorsJSONArray && authorsJSONArray.size() > 0) {
            List<String> authorList = new ArrayList<>();
            for (Object authorObj : authorsJSONArray) {
                JSONObject authorJson = (JSONObject) authorObj;
                String authorName = authorJson.containsKey("name_zh") ? authorJson.getString("name_zh") : null;
                if (StringUtils.isNotBlank(authorName))
                    authorList.add(authorName);
            }
            if (CollectionUtils.isNotEmpty(authorList))
                paper.setOtherAuthor(StringUtils.join(authorList, ","));
        }

        if (StringUtils.isNotBlank(doi) && StringUtils.isBlank(paper.getDoi()))
            paper.setDoi(doi);
        if (StringUtils.isNotBlank(title) && StringUtils.isBlank(paper.getTitle()))
            paper.setTitle(title);
        if (StringUtils.isNotBlank(publicationDate) && StringUtils.isBlank(paper.getPublicationDate()))
            paper.setPublicationDate(publicationDate);
        if (StringUtils.isNotBlank(abstractData) && StringUtils.isBlank(paper.getDescribeData()))
            paper.setDescribeData(abstractData);
    }

    /**
     * 外文
     *
     * @param jsonObject
     * @param paper
     */
    private static void parsePaperDataByEn(JSONObject jsonObject, Paper paper) {
        List urlList = jsonObject.containsKey("urls") ? jsonObject.getJSONArray("urls") : null;
        if (CollectionUtils.isNotEmpty(urlList) && StringUtils.isBlank(paper.getResourceLink()))
            paper.setResourceLink(StringUtils.join(urlList, ","));

        String doi = jsonObject.containsKey("doi") ? jsonObject.getString("doi") : null;
        // 发表日期
        String publicationDate = jsonObject.containsKey("year") ? jsonObject.getString("year") : null;
        // 摘要
        String abstractData = jsonObject.containsKey("abstract") ? jsonObject.getString("abstract") : null;
        // 标题
        String title = jsonObject.containsKey("title") ? jsonObject.getString("title") : null;

        JSONObject venueJSONObject = jsonObject.containsKey("venue") ? jsonObject.getJSONObject("venue") : null;
        if (null != venueJSONObject && venueJSONObject.size() > 0) {
            JSONObject venueInfoJSONObject = venueJSONObject.containsKey("info") ? venueJSONObject.getJSONObject("info") : null;
            if (null != venueInfoJSONObject && venueInfoJSONObject.size() > 0) {
                // 发表期刊
                String periodicalName = venueInfoJSONObject.containsKey("name") ? venueInfoJSONObject.getString("name") : null;
                // 出版人(或机构)
                String publisher = venueInfoJSONObject.containsKey("publisher") ? venueInfoJSONObject.getString("publisher") : null;
                if (StringUtils.isNotBlank(periodicalName))
                    paper.setPeriodical(periodicalName);
                if (StringUtils.isNotBlank(publisher))
                    paper.setPublisher(publisher);
            }
        }

        // 作者
        JSONArray authorsJSONArray = jsonObject.containsKey("authors") ? jsonObject.getJSONArray("authors") : null;
        if (null != authorsJSONArray && authorsJSONArray.size() > 0) {
            List<String> authorList = new ArrayList<>();
            for (Object authorObj : authorsJSONArray) {
                JSONObject authorJson = (JSONObject) authorObj;
                String authorName = authorJson.containsKey("name") ? authorJson.getString("name") : null;
                if (StringUtils.isNotBlank(authorName))
                    authorList.add(authorName);
            }
            if (CollectionUtils.isNotEmpty(authorList))
                paper.setOtherAuthor(StringUtils.join(authorList, ","));
        }

        if (StringUtils.isNotBlank(doi) && StringUtils.isBlank(paper.getDoi()))
            paper.setDoi(doi);
        if (StringUtils.isNotBlank(title) && StringUtils.isBlank(paper.getTitle()))
            paper.setTitle(title);
        if (StringUtils.isNotBlank(publicationDate) && StringUtils.isBlank(paper.getPublicationDate()))
            paper.setPublicationDate(publicationDate);
        if (StringUtils.isNotBlank(abstractData) && StringUtils.isBlank(paper.getDescribeData()))
            paper.setDescribeData(abstractData);
    }

    /**
     * 处理人物经历
     *
     * @param data
     * @param resumeList
     */
    public static void handlerResume(String data, List<Resume> resumeList) {
        if (data.indexOf("<br>") > 0) {
            String[] edus = data.split("<br>");
            for (int k = 0; k < edus.length; k++) {
                if (StringUtils.isNotBlank(edus[k])) {
                    Resume resume = new Resume().builder()
                            .resume(edus[k])
                            .build();
                    resumeList.add(resume);
                }
            }
        } else {
            Resume resume = new Resume().builder()
                    .resume(data)
                    .build();
            resumeList.add(resume);
        }
    }

    /**
     * 处理社会任职
     *
     * @param data
     * @param socialList
     */
    public static void handlerSocial(String data, List<Social> socialList) {
        if (data.indexOf("<br>") > 0) {
            String[] edus = data.split("<br>");
            for (int k = 0; k < edus.length; k++) {
                if (StringUtils.isNotBlank(edus[k])) {
                    Social social = new Social().builder()
                            .duties(edus[k])
                            .build();
                    socialList.add(social);
                }
            }
        } else {
            Social social = new Social().builder()
                    .duties(data)
                    .build();
            socialList.add(social);
        }
    }


    public static void main(String[] args) {
//        System.out.println(JSONObject.parseObject(aa));
//        System.out.println(StringUtils.isNumeric("1983.01.01"));
//        getAminerPaperAndPatent();
//        Document adminerDoc = CrawlerCommonUtils.getDocumentByFile("C:\\Users\\user\\Desktop\\561a886045cedb3397e83e7d.html");
        Document adminerDoc = CrawlerCommonUtils.getDocument("https://www.aminer.cn/profile/53f64553dabfaeaf590f3cce");
        System.out.println(adminerDoc.toString());
        getAminerHtmlScript(adminerDoc);

        // 全球华人专家库-杰青年度列表，personInfoParamsList里listId为杰青列表id,year为年度
//        List<Map<String, Object>> personInfoParamsList = aminerList();

//        List<PersonInfo> list = new JQCrawlerService().crawlerOutstanTalents();
//        Map<String, List<PersonInfo>> personInfoGroupMap = list.stream().collect(Collectors.groupingBy(PersonInfo::getElectionYear));
//        System.out.println(JSON.toJSONString(personInfoGroupMap));
//
//        personInfoGroupMap.forEach((yearValue, personInfoList) -> {
//            Map<String, PersonInfo> nameMap = new HashMap<>();
//            // 同年重名人员，2019:石峰、张龙，2014:张强，2009:汪国平，2007：王伟，暂不处理，也没有几个手动解决
//            if (StringUtils.equals(yearValue, "2019")) {
//                nameMap = personInfoList.stream().filter(personInfo -> null == personInfo.getStamp()
//                        && !StringUtils.equalsAny(personInfo.getName(), "石峰", "张龙"))
//                        .collect(Collectors.toMap(PersonInfo::getName, x -> x));
//            } else if (StringUtils.equals(yearValue, "2014")) {
//                nameMap = personInfoList.stream().filter(personInfo -> null == personInfo.getStamp()
//                        && !StringUtils.equalsAny(personInfo.getName(), "张强"))
//                        .collect(Collectors.toMap(PersonInfo::getName, x -> x));
//            } else if (StringUtils.equals(yearValue, "2009")) {
//                nameMap = personInfoList.stream().filter(personInfo -> null == personInfo.getStamp()
//                        && !StringUtils.equalsAny(personInfo.getName(), "汪国平"))
//                        .collect(Collectors.toMap(PersonInfo::getName, x -> x));
//            } else if (StringUtils.equals(yearValue, "2007")) {
//                nameMap = personInfoList.stream().filter(personInfo -> null == personInfo.getStamp()
//                        && !StringUtils.equalsAny(personInfo.getName(), "王伟"))
//                        .collect(Collectors.toMap(PersonInfo::getName, x -> x));
//            } else {
//                nameMap = personInfoList.stream().filter(personInfo -> null == personInfo.getStamp())
//                        .collect(Collectors.toMap(PersonInfo::getName, x -> x));
//            }
////            File outstandYouthFile = new File( outstandYouthPath + "youth-" + yearValue + ".txt");
//            File outstandYouthFile = new File("E:\\project\\crawler\\src\\main\\resources\\htmlTest\\jq-杰出青年\\" + "youth-" + yearValue + ".txt");
//            if (outstandYouthFile.exists()) {
//                try {
//                    List<String> yearYouthList = FileUtils.readLines(outstandYouthFile, StandardCharsets.UTF_8);
//                    for (String line : yearYouthList) {
//                        parseAminerTxt(line, nameMap, yearValue);
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            List<String> nameList = personInfoList.stream().map(PersonInfo::getName).collect(Collectors.toList());
//            List<String> nameList2 = nameMap.entrySet().stream().map(entry -> entry.getKey()).collect(Collectors.toList());
//            List<String> repearList = nameList.stream().filter(item -> !nameList2.contains(item)).collect(Collectors.toList());
//            List<String> repearList1 = nameList2.stream().filter(item -> !nameList.contains(item)).collect(Collectors.toList());
//
//
//            System.out.println(JSON.toJSONString(repearList));
//            System.out.println(JSON.toJSONString(repearList1));
//        });

    }

}
